//
//  CZCommentCell.swift
//  GZWeibo007
//
//  Created by Raymond on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SDWebImage
/// 评论cell
class CZCommentCell: UITableViewCell {
    
    var comment: CZComment?{
        didSet{
            let imageURL = NSURL(string: comment!.user!.profile_image_url!)
            iconView.sd_setImageWithURL(imageURL, placeholderImage: UIImage(named: "avatar"))
            nameLabel.text = comment!.user?.screen_name
            timeLabel.text = comment!.created_at
            contentLabel.text = comment!.text
            
        }
    }
    
    // 计算行高的方法
    func rowHeight(comment: CZComment) -> CGFloat {
        self.comment = comment
        self.layoutIfNeeded()
        let maxY = CGRectGetMaxY(separatorView.frame)
        return maxY
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func prepareUI() {
        contentView.addSubview(iconView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(contentLabel)
        contentView.addSubview(separatorView)
        
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: 35, height: 35), offset: CGPoint(x: 8, y: 8))
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 0))
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 0))
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: timeLabel, size: nil, offset: CGPoint(x: 0, y: 8))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - (35 + 3 * 8)))
        separatorView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: UIScreen.mainScreen().bounds.width - (35 + 3 * 8), height: 0.5), offset: CGPoint(x: 0, y: 8))
    }
    
    // MARK: - 懒加载
    /// 头像
    private lazy var iconView: UIImageView = UIImageView()
    /// 名字
    private lazy var nameLabel: UILabel = UILabel(color: UIColor.blackColor(), fontSize: 12)
    /// 日期
    private lazy var timeLabel: UILabel = UILabel(color: UIColor.orangeColor(), fontSize: 9)
    /// 内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.blackColor(), fontSize: 14)
        label.numberOfLines = 0
        label.sizeToFit()
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - (35 + 3 * 8)
        return label
    }()
    /// 分割线
    private lazy var separatorView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.lightGrayColor()
        return view
    }()
    

}
